<?php
namespace Zxf5115\Laravel\Dictionary\Logics;

use Illuminate\Http\Request;
use Zxf5115\Laravel\Dictionary\Services\DictionaryService;

/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2025-09-22
 *
 * 字典逻辑处理类
 */
class DictionaryLogic
{
  /**
   * 字典数据处理
   *
   * @param Request $request 用户请求参数
   * @param string $modelClass 操作模型类
   * @return void
   */
  public function handle(Request $request, string $modelClass) : void
  {
    $model = $modelClass::firstOrNew(['id' => $request->id]);

    $model->name = $request->name;
    $model->title = $request->title;
    $model->description = $request->description ?? '';
    $model->status = intval($request->status);
    $model->save();

    // 更新数据后清除对应缓存，保持一致性
    app(DictionaryService::class)->deleteCacheDataByTitle($request->title);
  }
}
